package com.example.bookstore.api

import com.example.bookstore.dao.UserDao
import com.example.bookstore.entity.CartDetailEntity
import com.example.bookstore.entity.UserEntity
import com.example.bookstore.response.SuccessResponse
import com.example.bookstore.service.UserService
import com.example.bookstore.validate.UserUpdateRestrict
import com.fasterxml.jackson.annotation.JsonView
import com.fasterxml.jackson.databind.ObjectMapper
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.validation.constraints.NotBlank

/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/8/10
 * Time: 15:14
 */
@Api(tags = ["用户"], basePath = "/api/user", description = "【需要认证信息】用户控制器需要登录认证信息，认证信息从请求消息头Authorization读取。服务器读取：Authorization: Bearer \$Token$")
@RestController
@RequestMapping("/api/user")
class UserController : BaseController<UserService, UserDao, UserEntity>() {

    @Autowired
    private lateinit var objectMapper: ObjectMapper

    @ApiOperation("获取用户信息", notes = "自动读取Token中的用户，返回Token中的用户信息")
    @JsonView(UserEntity.UserView::class)
    @GetMapping("/info")
    fun getInfo(): UserEntity {
        return service.getTokenUser()
    }

//    @JsonView(UserEntity.UserView::class)
//    override fun getAllPageable(@Validated pageNSizeValidate: PageNSizeValidate): MutableList<UserEntity> {
//        return super.getAllPageable(pageNSizeValidate)
//    }

    @ApiOperation("加入购物车", notes = "传入BookId，加入购物车")
    @ApiImplicitParams(ApiImplicitParam("JSON，包含加入购物车的BookID", name = "json", required = true, allowMultiple = false))
    @PostMapping("/cart")
    fun addToCart(@RequestBody json: String): SuccessResponse {
        val bookId = objectMapper.readTree(json).path("bookId").textValue()
        service.addToCart(bookId)
        return SuccessResponse("加入购物车成功")
    }

    @ApiOperation("读取用户购物车列表", notes = "读取Token中用户购物车列表")
    @JsonView(CartDetailEntity.CartDetailViewWithBook::class)
    @GetMapping("/cart")
    fun getCartList(): List<CartDetailEntity> {
        return service.getCartList().sortedBy { it.createTime }.reversed()
    }

    @ApiOperation("移出购物车", notes = "传入购物车内的ID，移出购物车")
    @ApiImplicitParams(ApiImplicitParam("CartDetail ID", name = "id", required = true, allowMultiple = false))
    @DeleteMapping("/cart/{id}")
    fun deleteCartItemById(@NotBlank(message = "id 不能为空") @PathVariable id: String?) {
        return service.deleteCartItemById(id!!)
    }

    @ApiOperation("清空用户购物车列表", notes = "清空Token中用户购物车列表")
    @DeleteMapping("/cart/clear")
    fun clearCart(): SuccessResponse {
        service.clearCart()
        return SuccessResponse("清空购物车成功")
    }

    @PutMapping("/info")
    @ApiOperation("更新用户信息", notes = "传入Json，更新Token用户信息")
    fun updateUserInfo(@RequestBody json: UserUpdateRestrict): SuccessResponse {
        service.updateUserInfo(json)
        return SuccessResponse("更新用户信息成功")
    }

}